package com.yanqu.road.dao.impl.activity.flowercard;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.flowercards.config.FlowerCardRoundInfo;
import com.yanqu.road.entity.activity.flowercards.config.FlowerCardScoreInfo;
import com.yanqu.road.entity.activity.flowercards.config.FlowerCardSkillInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FlowerCardConfigDaoImpl extends TempDao {

    private static String getFlowerCardScoreInfoSql = "select * from `t_s_activity_flowercard_score` where `activityId`=? ORDER BY `id` ASC";
    public List<FlowerCardScoreInfo> getFlowerCardScoreInfoList(int activityId) {
        List<FlowerCardScoreInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerCardScoreInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerCardScoreInfo data = new FlowerCardScoreInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getString("icon"));
                data.setName(rs.getString("name"));
                data.setScore(rs.getString("score"));
                data.setRewards(rs.getString("rewards"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    private static String getFlowerCardSkillInfoSql = "select * from `t_s_activity_flowercard_skill` where `activityId`=? ORDER BY `skillId` ASC";
    public List<FlowerCardSkillInfo> getFlowerCardSkillInfoList(int activityId) {
        List<FlowerCardSkillInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerCardSkillInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerCardSkillInfo data = new FlowerCardSkillInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setSkillId(rs.getInt("skillId"));
                data.setSkillName(rs.getString("skillName"));
                data.setSkillIntro(rs.getString("skillIntro"));
                data.setSkillDesc(rs.getString("skillDesc"));
                data.setSkillLevel(rs.getInt("skillLevel"));
                data.setSkillParam(rs.getString("skillParam"));
                data.setIcon(rs.getString("icon"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getFlowerCardRoundInfoSql = "select * from `t_s_activity_flowercard_rounds` where `activityId`=? ORDER BY `rounds` ASC";
    public List<FlowerCardRoundInfo> getFlowerCardRoundInfoList(int activityId) {
        List<FlowerCardRoundInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerCardRoundInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerCardRoundInfo data = new FlowerCardRoundInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setType(rs.getInt("type"));
                data.setRounds(rs.getInt("rounds"));
                data.setStepNum(rs.getInt("stepNum"));
                data.setBeautyNum(StringUtils.stringToIntegerList(rs.getString("beautyNum"), "\\|"));
                data.setNpcWeights(StringUtils.stringToIntegerList(rs.getString("npcWeights"), "\\|"));
                data.setNpcSufferance(StringUtils.stringToIntegerList(rs.getString("npcSufferance"), "\\|"));
                data.setNpcReward(StringUtils.stringToIntegerList(rs.getString("npcReward"), "\\|"));
                data.setUnlockSkills(StringUtils.stringToIntegerList(rs.getString("unlockSkills"), "\\|"));
                data.setBeautyList(StringUtils.stringToIntegerList(rs.getString("beautyList"), "\\|"));
                data.setNpcNum(rs.getInt("npcNum"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
